package com.ccsu.mymarket.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccsu.mymarket.dao.AuthorityDao;
import com.ccsu.mymarket.dao.RoleDao;
import com.ccsu.mymarket.entity.Role;
import com.ccsu.mymarket.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {
    private RoleDao roleDao;
    private AuthorityDao authorityDao;

    public RoleServiceImpl(@Autowired RoleDao roleDao, @Autowired AuthorityDao authorityDao) {
        this.roleDao = roleDao;
        this.authorityDao = authorityDao;
    }

    @Override
    public Boolean addRole(Role role, List<Integer> authoList) {
        try {
//            对自增主键，mybatisplus将插入后的id赋值给了传入的对象
            roleDao.insert(role);
            authorityDao.addRoleAuthoRel(authoList,role.getId());
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public Boolean deleteRoleById(Integer id) {
        try {
            authorityDao.deleteRoleAuthoRel(id);
            removeById(id);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public List<Role> getRoles() {
        return roleDao.getRoles();
    }
}
